package com.yc.dataStructure;

import java.util.concurrent.LinkedBlockingQueue;

/**
 * 测试阻塞队列
 * @author yuancan
 *
 */
public class TestBlockingQueue {
	
	/**
	 * 阻塞队列
	 */
	private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

	/**
	 * 生产者
	 * @author yuancan
	 *
	 */
	class Producer implements Runnable {

		@Override
		public void run() {
			for (int i = 0; i < 100; i++) {
				try {
					queue.put(i);
					System.out.println("Producer----" + i);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	/**
	 * 消费者
	 * @author yuancan
	 *
	 */
	class Consumer implements Runnable {

		@Override
		public void run() {
			for (int i = 0; i < 200; i++) {
				try {
					System.out.println("Consumer ---- " + queue.take());
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	
	public static void main(String[] args) {
		TestBlockingQueue tbq = new TestBlockingQueue();
		Thread consumer = new Thread(tbq.new Consumer());
		Thread producer = new Thread(tbq.new Producer());
		System.out.println("start consumer...");
		consumer.start();
		System.out.println("start producer...");
		producer.start();
		
		System.out.println("end!");
	}
	
	
}
